package com.finalx.mytp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;

public class ActivityDisplayKeyboard extends Activity {
	private static String TAG;// = getString( R.string.main_string_display) ;

	private ImageView mView = null;
	private int mResultCode = Activity_Main.TEST_FAIL;
	private int iColors[] = { Color.BLACK, Color.RED, Color.GREEN, Color.BLUE,
			Color.WHITE };
	private int currentColorIndex = 0;
	private WindowManager.LayoutParams mLayoutparams = null;
	private AlertDialog mAlertDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		TAG = getString(R.string.main_string_display_key);

		setContentView(R.layout.activity_display);
		mView = (ImageView) findViewById(R.id.display_view);
		mView.setBackgroundColor(Color.WHITE);

		mLayoutparams = getWindow().getAttributes();
		mLayoutparams.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
		mLayoutparams.screenBrightness = 1.0f;
		getWindow().setAttributes(mLayoutparams);

		final Intent resIntent = new Intent();
		resIntent.putExtra(TAG, mResultCode);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// Add the buttons
		builder.setPositiveButton(android.R.string.yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// yes
						mResultCode = Activity_Main.TEST_PASS;
						setResult(mResultCode, resIntent);
						finish();
					}
				});
		builder.setNegativeButton(android.R.string.no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						//no
						mResultCode = Activity_Main.TEST_FAIL;
						setResult(mResultCode, resIntent);
						finish();
					}
				});
		// Set other dialog properties
		builder.setTitle(R.string.ask_passorfail);
		// Create the AlertDialog
		mAlertDialog = builder.create();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK: {
			mLayoutparams.screenBrightness = -1.0f;
			getWindow().setAttributes(mLayoutparams);

			mAlertDialog.show();
			return true;
		}

		case KeyEvent.KEYCODE_VOLUME_UP: {
			mView.setBackgroundColor(previousColor());
			return true;
		}

		case KeyEvent.KEYCODE_VOLUME_DOWN: {
			mView.setBackgroundColor(nextColor());
			return true;
		}

		default: {
			return super.onKeyDown(keyCode, event);
		}
		}
	}

	public int nextColor() {
		currentColorIndex++;
		currentColorIndex %= iColors.length;
		return iColors[currentColorIndex];
	}

	public int previousColor() {
		currentColorIndex--;
		currentColorIndex = (currentColorIndex < 0) ? (iColors.length - 1)
				: currentColorIndex;
		return iColors[currentColorIndex];
	}

}
